package com.study.zip;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class ZipConpress {
    static int bufferSize = 8192;//单位bytes

    /**
     * 用于单文件压缩
     */
    public static void doCompress(File srcFile, File destFile) throws IOException {
        ZipArchiveOutputStream out = null;
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize);
            out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize));
            ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
            entry.setSize(srcFile.length());
            out.putArchiveEntry(entry);
            IOUtils.copy(is, out);
            out.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 用于多文件压缩
     */
    public static void doCompressFiles(List<File> srcFiles, File destFile) throws IOException {
        ZipArchiveOutputStream out = null;
        InputStream is = null;
        try {

            out = new ZipArchiveOutputStream(new BufferedOutputStream(new FileOutputStream(destFile), bufferSize));

            for (File srcFile : srcFiles) {
                is = new BufferedInputStream(new FileInputStream(srcFile), bufferSize);
                ZipArchiveEntry entry = new ZipArchiveEntry(srcFile.getName());
                entry.setSize(srcFile.length());
                out.putArchiveEntry(entry);
                IOUtils.copy(is, out);
            }
            out.closeArchiveEntry();
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(out);
        }
    }

    public static void doDecompress(File srcFile, File destDir) throws IOException {
        ZipArchiveInputStream is = null;
        try {
            is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), bufferSize));
            ZipArchiveEntry entry = null;
            while ((entry = is.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(destDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(
                                new FileOutputStream(new File(destDir, entry.getName())), bufferSize);
                        IOUtils.copy(is, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }
        } finally {
            IOUtils.closeQuietly(is);
        }
    }

    public static void main(String[] args) {
        try {
            //ZipConpress.doCompress(new File("/pathto/abc.txt"),new File("/pathto/abc-test.zip"));

            List<File> files = new ArrayList<File>();
            files.add(new File("/pathto/abc.txt"));
            files.add(new File("/pathto/xyz.txt"));


            ZipConpress.doCompressFiles(files, new File("/pathto/abc-test.zip"));


//            File doDecompressDir = new File("/pathto/abc-test1723");
//            if (!doDecompressDir.exists()) {
//                doDecompressDir.mkdirs();
//            }
//            ZipConpress.doDecompress(new File("/pathto/abc-test.zip"), doDecompressDir
//            );

        } catch (IOException e) {

            e.printStackTrace();
        }


    }
}

